﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TextRPG.Models;

namespace TextRPG.Controllers
{ 
    public class ItemDefinitionController : Controller
    {
        private RPGContext db = new RPGContext();

        //
        // GET: /ItemDefinition/

        public ViewResult Index()
        {
            var itemdefinitions = db.ItemDefinitions.Include(i => i.ItemType).Include(i => i.WeaponSubClass).Include(i => i.WeaponClass).Include(i => i.DamageType).Include(i => i.ArmorClass);
            return View(itemdefinitions.ToList());
        }

        //
        // GET: /ItemDefinition/Details/5

        public ViewResult Details(int id)
        {
            ItemDefinition itemdefinition = db.ItemDefinitions.Find(id);
            return View(itemdefinition);
        }

        //
        // GET: /ItemDefinition/Create

        public ActionResult Create()
        {
            ViewBag.ItemTypeID = new SelectList(db.ItemTypes, "ItemTypeID", "Name");
            ViewBag.WeaponSubClassID = new SelectList(db.WeaponSubClasses, "WeaponSubClassID", "Name");
            ViewBag.WeaponClassID = new SelectList(db.WeaponClasses, "WeaponClassID", "Name");
            ViewBag.DamageTypeID = new SelectList(db.DamageTypes, "DamageTypeID", "Name");
            ViewBag.ArmorClassID = new SelectList(db.ArmorClasses, "ArmorClassID", "Name");
            return View();
        } 

        //
        // POST: /ItemDefinition/Create

        [HttpPost]
        public ActionResult Create(ItemDefinition itemdefinition)
        {
            if (ModelState.IsValid)
            {
                db.ItemDefinitions.Add(itemdefinition);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.ItemTypeID = new SelectList(db.ItemTypes, "ItemTypeID", "Name", itemdefinition.ItemTypeID);
            ViewBag.WeaponSubClassID = new SelectList(db.WeaponSubClasses, "WeaponSubClassID", "Name", itemdefinition.WeaponSubClassID);
            ViewBag.WeaponClassID = new SelectList(db.WeaponClasses, "WeaponClassID", "Name", itemdefinition.WeaponClassID);
            ViewBag.DamageTypeID = new SelectList(db.DamageTypes, "DamageTypeID", "Name", itemdefinition.DamageTypeID);
            ViewBag.ArmorClassID = new SelectList(db.ArmorClasses, "ArmorClassID", "Name", itemdefinition.ArmorClassID);
            return View(itemdefinition);
        }
        
        //
        // GET: /ItemDefinition/Edit/5
 
        public ActionResult Edit(int id)
        {
            ItemDefinition itemdefinition = db.ItemDefinitions.Find(id);
            ViewBag.ItemTypeID = new SelectList(db.ItemTypes, "ItemTypeID", "Name", itemdefinition.ItemTypeID);
            ViewBag.WeaponSubClassID = new SelectList(db.WeaponSubClasses, "WeaponSubClassID", "Name", itemdefinition.WeaponSubClassID);
            ViewBag.WeaponClassID = new SelectList(db.WeaponClasses, "WeaponClassID", "Name", itemdefinition.WeaponClassID);
            ViewBag.DamageTypeID = new SelectList(db.DamageTypes, "DamageTypeID", "Name", itemdefinition.DamageTypeID);
            ViewBag.ArmorClassID = new SelectList(db.ArmorClasses, "ArmorClassID", "Name", itemdefinition.ArmorClassID);
            return View(itemdefinition);
        }

        //
        // POST: /ItemDefinition/Edit/5

        [HttpPost]
        public ActionResult Edit(ItemDefinition itemdefinition)
        {
            if (ModelState.IsValid)
            {
                db.Entry(itemdefinition).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.ItemTypeID = new SelectList(db.ItemTypes, "ItemTypeID", "Name", itemdefinition.ItemTypeID);
            ViewBag.WeaponSubClassID = new SelectList(db.WeaponSubClasses, "WeaponSubClassID", "Name", itemdefinition.WeaponSubClassID);
            ViewBag.WeaponClassID = new SelectList(db.WeaponClasses, "WeaponClassID", "Name", itemdefinition.WeaponClassID);
            ViewBag.DamageTypeID = new SelectList(db.DamageTypes, "DamageTypeID", "Name", itemdefinition.DamageTypeID);
            ViewBag.ArmorClassID = new SelectList(db.ArmorClasses, "ArmorClassID", "Name", itemdefinition.ArmorClassID);
            return View(itemdefinition);
        }

        //
        // GET: /ItemDefinition/Delete/5
 
        public ActionResult Delete(int id)
        {
            ItemDefinition itemdefinition = db.ItemDefinitions.Find(id);
            return View(itemdefinition);
        }

        //
        // POST: /ItemDefinition/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            ItemDefinition itemdefinition = db.ItemDefinitions.Find(id);
            db.ItemDefinitions.Remove(itemdefinition);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}